#include "drv_sonsor_air.h"
#include "SC_Init.h"
#include "stdio.h"
#include "string.h"
#include "toolkit.h"

#define amRevBufSizeMax 40

uint16_t xdata rcvStatSensor = 0;
uint16_t xdata recvLengthSensor = 0;

extern bit UartXSendFlag;
extern bit UartXReceiveFlag;

uint8_t xdata amRcv[amRevBufSizeMax] = {0};
uint8_t code pm2107WorkOnCmd[] = {0x11, 0x03, 0x0c, 0x02, 0x1e, 0xc0};
uint8_t code pm2107WorkOffCmd[] = {0x11, 0x03, 0x0c, 0x01, 0x1e, 0xc1};
uint8_t code pm2107ReadValCmd[] = {0x11, 0x02, 0x0b, 0x01, 0xe1};

uint8_t am1008CheckSum = 0;

//extern uint8_t xdata pm2d5DebugFlag;
//extern uint16_t xdata pM2D5DebugValue;

uint16_t xdata pM2D5Value = 0;
static uint8_t pmWorkStatus = 1; 

//uint8_t pm2d5ReadFlag = 0;
uint16_t pm2d5CmdRequest = 0;
uint8_t pm2d5CmdResponse = 0;
uint32_t xdata pm2d5WaitTick = 0;
uint32_t xdata pm2d5ReadTick = 0;
uint32_t xdata pm2d5PreheatTick = 0;

uint16_t xdata pm2d5ReadReqCount = 0;
uint16_t xdata pm2d5ReadErrCount = 0;
uint16_t xdata pm2d5ReadErrBefore = 0;
uint8_t pm2d5ErrTimes = 0;

uint16_t xdata printVal = 0;


void sensor_air_init(void)
{
	SC_USCI2_Init();
	
}

void usart_pm2d5_receive(uint8_t recv)
{
    if(rcvStatSensor == 0x01){
        if(recv == 0x16){
            amRcv[0] = recv;
            rcvStatSensor = 0x02;			
        }
    }else
    if(rcvStatSensor == 0x02){
        amRcv[1] = recv;
        rcvStatSensor = 0x03;
				recvLengthSensor = 0;
    }else
    if(rcvStatSensor == 0x03){
        amRcv[2+recvLengthSensor] = recv;
				recvLengthSensor++;
				if(recvLengthSensor >= amRcv[1])
					rcvStatSensor = 0x04;
    }else
    if(rcvStatSensor == 0x04){
        amRcv[2+recvLengthSensor] = recv;
        rcvStatSensor = 0x05;
    }else
		{
        rcvStatSensor = 0x06;
    }
}

void uart2SendByte(uint8_t Data)
{
  USXCON3 = Data;
	while(!UartXSendFlag);
	UartXSendFlag = 0;
 // return c;
}

void usart2_transmit_datas(unsigned char* sendBuffer, unsigned int sendSize)
{
	uint32_t count = sendSize;
	while(count > 0){
			uart2SendByte(sendBuffer[sendSize - count]);
			count--;
	}
}

 void sensor_air_data_handle(void)
{
	uint32_t i,sum;

	if(pm2d5CmdRequest == 1){
		usart2_transmit_datas(pm2107ReadValCmd, sizeof(pm2107ReadValCmd));
		pm2d5CmdRequest = 0;
		pm2d5CmdResponse = 1;
		pm2d5WaitTick = 0;
		rcvStatSensor = 0x01;
	}else
	if(pm2d5CmdRequest == 2){
		usart2_transmit_datas(pm2107WorkOnCmd, sizeof(pm2107WorkOnCmd));
		pm2d5CmdRequest = 0;
		pm2d5CmdResponse = 2;
		pm2d5WaitTick = 0;
		rcvStatSensor = 0x01;
		//pmWorkStatus = 1;
//		printf("am1008 work on cmd\r\n");
	}else
	if(pm2d5CmdRequest == 3){
		usart2_transmit_datas(pm2107WorkOffCmd, sizeof(pm2107WorkOffCmd));
		pm2d5CmdRequest = 0;
		pm2d5CmdResponse = 3;	
		pm2d5WaitTick = 0;
		rcvStatSensor = 0x01;
	//	printf("am1008 work off cmd\r\n");
		//pmWorkStatus = 0;
	}
	
	if(rcvStatSensor == 0x05)
	{
			sum = 0;
			for( i = 0 ; i <= recvLengthSensor+2 ; i++)
			{
					sum += amRcv[i];
					sum &= 0x00ff;
			}
			
			if(sum == 0){
					if(amRcv[2] == 0x0b)
					{
						pM2D5Value = (256 *  amRcv[5]) + amRcv[6];
						if(pm2d5CmdResponse == 1)
							pm2d5CmdResponse = 0;
						if(pm2d5ReadErrBefore!=pm2d5ReadErrCount)
						{
							//printf("pm2d5ReadReqCount:%d,pm2d5ReadErrCount:%d Value:%d\r\n", pm2d5ReadReqCount, pm2d5ReadErrCount, pM2D5Value);
							pm2d5ReadErrBefore=pm2d5ReadErrCount;
						}
						//printf("am1008 ok:%d\r\n", pM2D5Value);
					}
					else
					if(amRcv[2] == 0x0c)
					{
						if(amRcv[3] == 0x02){
							if(pm2d5CmdResponse == 2)
								pm2d5CmdResponse = 0;
								pmWorkStatus = 1;
								pm2d5PreheatTick = 0;
								pM2D5Value = 0;
								pm2d5ReadTick = 0;
							//	printf("am1008 work on\r\n");
						}else
						if(amRcv[3] == 0x01){
							if(pm2d5CmdResponse == 3)
								pm2d5CmdResponse = 0;
								pmWorkStatus = 0;
								pm2d5PreheatTick = 0;
								pM2D5Value = 0;
							//	printf("am1008 work off\r\n");
						}
					}
					pm2d5ErrTimes = 0;
					
			}else{
					memset(amRcv, 0x00, sizeof(amRcv));
				//	printf("am1008 checksum failed\r\n");
			}
			
			rcvStatSensor = 0x00;
			
	}else
	if(rcvStatSensor== 0x06)
	{
			memset(amRcv, 0x00, sizeof(amRcv));
		printVal = rcvStatSensor;
	//	printf("am1008 receive failed:%d\r\n", printVal);
			rcvStatSensor = 0x00;
			//printf("am1008 receive failed:%d\r\n", rcvStatSensor);
			//amRest();
	}
	
	if(pm2d5CmdResponse != 0){
		if(device_timeout_count(&pm2d5WaitTick, 1000) ==  TIME_OUT_WORK){
			//printf("am1008 receive time out, pm2d5CmdResponse:%d\r\n", pm2d5CmdResponse);
			if(pm2d5ErrTimes < 5)
				pm2d5ErrTimes++;
			if(pm2d5CmdResponse == 1){
				pm2d5ReadErrCount++;
			}
			if(pm2d5CmdResponse == 2)
				pm2d5CmdRequest = 2;
			if(pm2d5CmdResponse == 3)
				pm2d5CmdRequest = 3;
			pm2d5CmdResponse = 0;
		}
	}
	
	if(pmWorkStatus == 1)
	{
		if(device_timeout_count(&pm2d5PreheatTick, 2000) ==  TIME_OUT_WORK){
			if(pm2d5ErrTimes == 0){
				pmWorkStatus = 2;
			//	printf("am1008 preheat ok\r\n");			
			}

		}
	}
	
	if(device_timeout_count(&pm2d5ReadTick, 980) ==  TIME_OUT_WORK){
		if(pmWorkStatus != 0){
			pm2d5CmdRequest = 1;
			pm2d5ReadReqCount++;		
		}
		//printf("am1008 request code:%d\r\n", pm2d5CmdRequest);			
	}
}

void sensor_air_work_control(uint8_t state)
{
	if(state == 0){
		if(pmWorkStatus != 0)
		{
			pm2d5CmdRequest = 3;
		}
	}else{
		if(pmWorkStatus == 0)
		{
			pm2d5CmdRequest = 2;
		}		
	}
}

uint16_t sensor_air_info_get(void)
{
	if(pm2d5ErrTimes >= 5)
		return 0;
	
	if(pmWorkStatus != 2)
		return 0;
	
	if(pM2D5Value == 0)
		return 1;
	
//	if(pm2d5DebugFlag == 0xaa)
//		return pM2D5DebugValue;
//	else
		return pM2D5Value;
}

